package com.stn.zjx.givingtreasure.activity.userprofile;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.StatusDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.UserNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;
import com.stn.zjx.givingtreasure.table.User;
import com.vondear.rxtools.RxRegTool;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ModifyUserEmailActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.del_bt)
    RelativeLayout delBt;
    @BindView(R.id.content_edit)
    EditText contentEdit;

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
            actionTitle.setText(getString(R.string.email));
            sureBt.setVisibility(View.VISIBLE);
            initLocationUser();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_modfiy_email);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt,R.id.sure_bt, R.id.del_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
            case R.id.sure_bt:

               if(!RxRegTool.isEmail(contentEdit.getText().toString().trim())){
                            showToast(getString(R.string.please_input_email));
                          return;
                  }

                updateProfile(contentEdit.getText().toString().trim());
                break;
            case R.id.del_bt:
                contentEdit.getText().clear();
                break;
        }
    }
    public void initLocationUser(){
        if (UserService.getInstance(this).getOnlineLoginUser() == null) {
            finish();
            return;
        }
        User userOnline = UserService.getInstance(this).getOnlineLoginUser();
        contentEdit.setText(userOnline.getUserEmail());
        if(!TextUtils.isEmpty(contentEdit.getText())){
            contentEdit.setSelection(contentEdit.getText().length());
        }
    }

    public void updateProfile(String userEmail){
      if (UserService.getInstance(this).getOnlineLoginUser() == null) {
            return;
        }
        User userOnline = UserService.getInstance(this).getOnlineLoginUser();
        JSONObject data=new JSONObject();
        try {
            data.put("userEmail",userEmail);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        UserNetImpl
                .updateuserprofile_bytoken(data.toString(),userOnline.getToken(), "updateuserprofile_bytoken", new StringCallback() {
                    @Override
                    public void onFinish() {
                        super.onFinish();
                        dissmissLoading();
                    }

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        showLoading();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                          if(response.isSuccessful()){
                              StatusDomain domain=new Gson().fromJson(response.body(),StatusDomain.class);
                               switch (domain.getStatus()){
                                   case BaseApi.REQUEST_SUCCESS_CODE:
                                        finish();
                                       break;
                                  default:
                                      showToast(domain.getMessage());
                                      break;
                               }

                          }

                    }
                });
    }
}
